#include <func.h>

void stat_display(const struct stat* sb)
{
    printf("st_dev=%ld, st_ino=%ld, st_mode=%o, st_nlink=%ld, st_uid=%u, "
           "st_gid=%u, st_rdev=%ld, st_size=%ld, st_blksize=%ld, st_blocks=%ld, "
           "st_atime=%ld, st_mtime=%ld, st_ctime=%ld\n", 
           sb->st_dev,
           sb->st_ino,
           sb->st_mode,
           sb->st_nlink,
           sb->st_uid,
           sb->st_gid,
           sb->st_rdev,
           sb->st_size,
           sb->st_blksize,
           sb->st_blocks,
           sb->st_atime,
           sb->st_mtime,
           sb->st_ctime);

}

int main(int argc, char* argv[])
{
    // ./t_fstat file
    if (argc != 2) {
        fprintf(stderr, "Usage: %s file\n", argv[0]);
        exit(1);
    }

    // 1. 打开文件描述符
    int fd = open(argv[1], O_RDONLY);
    if (fd == -1) {
        perror("open()");
        exit(1);
    }

    // 调用 fstat 获取文件的元数据
    struct stat sb;
    if (fstat(fd, &sb)) {
        perror("fstat()");
        exit(1);
    }
    
    stat_display(&sb);
    return 0;
}

